from datetime import date
from django.http import HttpResponse
from django.shortcuts import render_to_response
from calendar import HTMLCalendar

#------------------------------------------------------------------------------
#  customCalendar
#------------------------------------------------------------------------------
class WorkoutCalendar(HTMLCalendar):
    def __init__(self, workouts):
        super(WorkoutCalendar, self).__init__()
        #self.workouts = self.group_by_day(workouts)
        self.workouts = workouts

    def formatday(self, day, weekday):
        if day != 0:
            cssclass = self.cssclasses[weekday]
            if date.today() == date(self.year, self.month, day):
                cssclass += ' today'
            if day in self.workouts:
                cssclass += ' filled'
                body = []
                for workout in self.workouts[day]:
                    body.append('<a href="hip hop link"> Hippy hop')
                    body.append('</a><br/>')
                return self.day_cell(cssclass, '<div class="dayNumber">%d</div> %s' % (day, ''.join(body)))
            return self.day_cell(cssclass, '<div class="dayNumber">%d</div>' % day)
        return self.day_cell('noday', '&nbsp;')
#------------------------------------------------------------------------------
#  year/month will be passed in the view function
#------------------------------------------------------------------------------

    def formatmonth(self, year, month):
        self.year, self.month = year, month
        return super(WorkoutCalendar, self).formatmonth(year, month)

    def day_cell(self, cssclass, body):
        return '<td class="%s">%s</td>' % (cssclass, body)

#------------------------------------------------------------------------------

def julio(request):
    f = open('c:/ayoub/dev/sandbox/varios/cal/calendario.css','r+')
    cssfile = f.read()
    f.close()
    workouts = {1:'jazz', 2:'swing',7:'salsa'}
    jul = WorkoutCalendar(workouts)
    html = '<html><head>%s</head><body><div id ="calendar">%s</div></body></html>' % (cssfile,jul.formatmonth(2011,7))
    return HttpResponse(html)
    #return HttpResponse(jul.formatmonth(2011,7))


